package com.guo.core.framework.tomcatStudy;

import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Locale;

/**
 * @projectName: 接口梳理
 * @package: com.guo.core.framework.tomcatStudy
 * @className: Response
 * @author: zhangcan30
 * @description: TODO
 * @date: 2023/6/25 16:25
 * @version: 1.0
 */
public class Response implements ServletResponse {
    private static final int BUGGER_SIZE = 1024;
    Request request;
    OutputStream output;
    PrintWriter writer;

    public Response(OutputStream output) {
        this.output = output;
    }
    public void setRequest(Request request){
        this.request =request;
    }
    public void sendStaticResource() throws IOException {
        byte[] bytes=new byte[BUGGER_SIZE];
        FileInputStream fis=null;

        try {
            File file=new File(HttpServer.WEB_ROOT,request.getUri());
            if (file.exists()){
                fis=new FileInputStream(file);
                int ch = fis.read(bytes, 0, BUGGER_SIZE);
                while (ch!=-1){
                    output.write(bytes,0,ch);
                }
            }else {
                String errorMessage="404 file not find";
                output.write(errorMessage.getBytes());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fis!=null){
                fis.close();
            }
        }

    }


    @Override
    public String getCharacterEncoding() {
        return null;
    }

    @Override
    public String getContentType() {
        return null;
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        return null;
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        writer=new PrintWriter(output,true);
        return writer;
    }

    @Override
    public void setCharacterEncoding(String s) {

    }

    @Override
    public void setContentLength(int i) {

    }

    @Override
    public void setContentLengthLong(long l) {

    }

    @Override
    public void setContentType(String s) {

    }

    @Override
    public void setBufferSize(int i) {

    }

    @Override
    public int getBufferSize() {
        return 0;
    }

    @Override
    public void flushBuffer() throws IOException {

    }

    @Override
    public void resetBuffer() {

    }

    @Override
    public boolean isCommitted() {
        return false;
    }

    @Override
    public void reset() {

    }

    @Override
    public void setLocale(Locale locale) {

    }

    @Override
    public Locale getLocale() {
        return null;
    }
}
